home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14170 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: reversing a string
  5. Date: Fri, 12 Apr 96 12:20:53 GMT
  6. Organization: none
  7. Message-ID: <829311653snz@genesis.demon.co.uk>
  8. References: <4k6cjl$j8f@central.server.swt.edu> <4k6jks$fh9@solutions.solon.com> <DpLtt5.Lqu@iquest.net> <829135845snz@genesis.demon.co.uk> <DpnFtK.C1w@iquest.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <DpnFtK.C1w@iquest.net> dlmiller@iquest.net "Doug Miller" writes:
  15.  
  16. >Lawrence Kirby <fred@genesis.demon.co.uk> wrote:
  17. >>In article <DpLtt5.Lqu@iquest.net> dlmiller@iquest.net "Doug Miller" writes:
  18. >>
  19. >>>seebs@solutions.solon.com (Peter Seebach) wrote:
  20. >>>>I can't see a way to reverse in place without a temporary of some sort,
  21. >>>>or a loop of some sort, or something which is fundementally equivalent
  22. >>>>to one of those.  There may be one, but I don't know it.
  23. >>>>
  24. >>>>-s
  25. >>>
  26. >>>How about this:
  27. >>>
  28. >>>void swap (char *s) {
  29. >>>    if (strlen(s) > 1) {
  30. >>
  31. >>strlen() is fundamentally based on a loop.
  32. >
  33. >OK, since the objective is to determine if the string is more than 1 character
  34. > long, we can do
  35. >the same by testing the next character.
  36. >Replace
  37. >        if (strlen(s) > 1)
  38. >with
  39. >        if (*(s + 1))
  40. >and the rest of the algorithm is still valid.
  41.  
  42. OK, you can do that with this particular strlen() but there are several
  43. others in the function that can't be replaced in this way since they are
  44. used to reference characters at the end of the string.
  45.  
  46. -- 
  47. -----------------------------------------
  48. Lawrence Kirby | fred@genesis.demon.co.uk
  49. Wilts, England | 70734.126@compuserve.com
  50. -----------------------------------------
  51.